package com.cn.wanxi.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.service.NewsService;
import com.cn.wanxi.service.ProductService;
import com.cn.wanxi.service.impl.CompanyServiceImpl;
import com.cn.wanxi.service.impl.NavServiceImpl;
import com.cn.wanxi.service.impl.NewsServiceImpl;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.vo.HomeModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/home")
public class HomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到签到传递过来的数据


//        3.将这些封装成对象
        ProductModel productModel = new ProductModel();
//        首页默认isShow为1
        productModel.setWhereShow(1);
        productModel.setPage(1);
        productModel.setLimit(2);


        NewsModel newsModel = new NewsModel();
        newsModel.setWhereShow(1);
        newsModel.setPage(1);
        newsModel.setLimit(3);

//        4.调用服务逻辑层

        ProductService productService = new ProductServiceImpl();
        NewsService newsService = new NewsServiceImpl();
//        5.得到结果信息

//        得到产品信息
        List<ProductModel> productModelList = productService.getProductModelList(productModel);
//        List<ProductModel> productModelListb = productService.getProductModelListb();
//        得到新闻信息
        List<NewsModel> newsModelList = newsService.getNewsModelList(newsModel);
//        第三种方式：第四阶段（推荐）
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("productModelList", productModelList);
        jsonObject.put("newsModelList", newsModelList);
//        jsonObject.put("productModelListb", productModelListb);
//        第二种方式，返回给前端（第四阶段可以用）
//        homeModel.setCompanyModel(companyModel);
//        homeModel.setNavModelList(navModelList);
//        第一种方式返回给前端（第二阶段可以用）
//        req.setAttribute("companyModel",companyModel);
//        req.setAttribute("navModelList",navModelList);
//        6.返回前端
//        resp.getWriter().println(JSONObject.toJSONString(homeModel));
        resp.getWriter().println(jsonObject);

    }
}
